﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightSdms.AdminService;
using System.Globalization;

namespace SilverlightSdms.UserControls
{
    public partial class UserControlControlPayCheckSummary : UserControl
    {
        public int UserId { get; private set; }
        public DateTime DateOfPayCheck { get; private set; }
        UsersServiceClient m_UsersServiceClient = new UsersServiceClient();
        SilverlightSdms.AdminService.User m_User;
        List<SilverlightSdms.AdminService.Visit> m_Visits;
        SilverlightSdms.AdminService.Paycheck m_Paycheck;

        public UserControlControlPayCheckSummary()
        {
            InitializeComponent();
            m_UsersServiceClient.GetUserCompleted += (s, ea) =>
                {
                    m_User = ea.Result;
                    FillData();
                };
        }

        public void Init(int UserId, DateTime DateOfPayCheck)
        {
            this.UserId = UserId;
            this.DateOfPayCheck = DateOfPayCheck;
            m_UsersServiceClient.GetUserAsync(UserId);
        }

        private void FillData()
        {
            try
            {
                m_Visits = m_User.Visits.Where(v => v.StartTime.Month == DateOfPayCheck.Month && v.StartTime.Year == DateOfPayCheck.Year).ToList();
            }
            catch
            {
                m_Visits = null;
            }
            try
            {
                m_Paycheck = m_User.Paychecks.First(p => p.ForMonth == DateOfPayCheck.Month);
            }
            catch
            {
                m_Paycheck = null;
            }
            
            txtBlockNote.Visibility = (m_Paycheck == null && m_Visits != null && m_Visits.Count > 0) ? System.Windows.Visibility.Visible : System.Windows.Visibility.Collapsed;
            
            if (m_Visits != null && m_Visits.Count > 0)
            {
                this.LayoutRoot.Visibility = System.Windows.Visibility.Visible;
                this.txtBlockError.Visibility = System.Windows.Visibility.Collapsed;
                string monthName = CultureInfo.CurrentCulture.DateTimeFormat.MonthNames[DateOfPayCheck.Month - 1];
                this.txtBlockSummaryTitle.Text = "PayCheck Summary " + monthName + " ," + DateOfPayCheck.Year;              
                this.txtBlockCompletedVisitsValue.Text = m_Visits.Count(v => v.IsDone == true).ToString();
                this.txtBlockUnCompletedVisitsValue.Text = m_Visits.Count(v => v.IsDone == false).ToString();

                int doneHours, doneMinuts;
                int undoneHours, undoneMinuts;
                CalcHours(true, out doneHours, out doneMinuts);
                CalcHours(false, out undoneHours, out undoneMinuts);

                int totalHours = doneHours + undoneHours;
                int totalmin = doneMinuts + undoneMinuts;
                if (totalmin / 60 != 0)
                {
                    totalHours += ((int)(totalmin / 60));
                    totalmin = totalmin % 60;
                }

                this.txtBlockUncompletedHoursValue.Text = undoneHours.ToString() + "h " + undoneMinuts.ToString() + "min";
                this.txtBlockDoneHoursValue.Text = doneHours.ToString() + "h " + doneMinuts.ToString() + "min";
                this.txtBlockTotalHoursValue.Text = totalHours.ToString() + "h " + totalmin.ToString() + "min";
                this.txtBlockHourlySalaryValue.Text = m_Paycheck == null ? m_Visits[0].User.HourlySalary.ToString() : m_Paycheck.HourlySalary.ToString();
                this.txtBlockMonthlyBonusValue.Text = m_Paycheck == null ? "No Value" : m_Paycheck.Bonus.ToString();
                this.txtBlockFineValue.Text = m_Paycheck == null ? "No Value" : m_Paycheck.Fine.ToString();
                this.txtBlockMinus.Text = m_Paycheck == null ? "No Value" : this.txtBlockFineValue.Text;
                this.txtBlockPlus.Text = m_Paycheck == null ? "No Value" : (doneMinuts * m_Paycheck.HourlySalary / 60 + m_Paycheck.Bonus + m_Paycheck.HourlySalary * doneHours).ToString();
                this.txtBlockTotalValue.Text = m_Paycheck == null ? "No Value" : (doneMinuts * m_Paycheck.HourlySalary / 60 + m_Paycheck.Bonus + m_Paycheck.HourlySalary * doneHours - m_Paycheck.Fine).ToString();
            }
            else //if (m_Paycheck != null)
            {
                this.LayoutRoot.Visibility = System.Windows.Visibility.Collapsed;
                this.txtBlockError.Visibility = System.Windows.Visibility.Visible;
                if (m_Visits == null || m_Visits.Count < 1)
                {
                    txtBlockError.Text = "No Visits For Current Month";
                }
                else
                {
                    txtBlockError.Text = "No PayCheck For Current Month";
                }
            }
        }

        private void CalcHours(bool IsDone,out int Hours, out int Minuts)
        {
            var hours = from v in m_Visits
                        where v.IsDone == IsDone
                        select (v.EndTime - v.StartTime);
            Hours = 0;
            Minuts = 0;
            foreach (var item in hours)
            {
                Hours += item.Hours;
                Minuts += item.Minutes;
                if (Minuts / 60 != 0)
                {
                    Hours += ((int)(Minuts / 60));
                    Minuts = Minuts % 60;
                }

            }
        }
    }
}
